/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package upeu.edu.pe.dao;

import java.math.BigDecimal;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import upeu.edu.pe.modelo.SauEstandar;

/**
 *
 * @author pc
 */
@Repository("EstandarDao")
public class EstandarDaoImplement implements EstandarDao{
    @Autowired
    public SessionFactory sessionFactory;
    
    @Override
    public List<SauEstandar> listaEstandar(){
        return sessionFactory.getCurrentSession().createCriteria(SauEstandar.class).list();
    }
    
    @Override
    public void insertarEstandar(SauEstandar estandar){
        System.out.println(estandar.getNombre());
        Session session =sessionFactory.openSession();
        session.beginTransaction().begin();
        session.save(estandar);
        session.beginTransaction().commit();
    }
    
    @Override
    public void eliminarEstandar(String idEstandar){
        sessionFactory.getCurrentSession()
                .createQuery("delete SauEstandar per where per.idEstandar=?")
                .setString(0, idEstandar)
                .executeUpdate();      
    }
    
    @Override
    public SauEstandar buscarIdEstandar(int idEstandar){
        return (SauEstandar)sessionFactory.getCurrentSession().get(SauEstandar.class, BigDecimal.valueOf((long)idEstandar));        
    }
    
    @Override
    public void actualizarEstandar(SauEstandar estandar){
        Session sesion=sessionFactory.openSession();
        System.out.println("---------->"+estandar.getFecha());
        try {
            sesion.beginTransaction().begin();
            sesion.update(estandar);
            sesion.getTransaction().commit();
        } catch (Exception e) { System.out.println("--------------"+e.getMessage());
        }
    }
    
}
